$(document).ready(function(){
	getArticleCategory();
});

function getArticleCategory(){
	$('#tableid').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/article/get-article-category',
            type:'post',
            data:function(d){
            },
            error:getError
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "category_title", "bSortable": false },
            { "data": "id", "bSortable": false },
            { "data": "sort", "bSortable": true },
            { "data": "use_flg", "bSortable":false },
            { "data": "is_display", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="changeArticleCategory('+ full.id +')">修改</a>';
                }
            },
            {
                "targets": [3],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<a href="javascript:" onclick="chooseSeo('+ full.id +',3)">修改</a>';
                }
            },
            {
                "targets": [5],
                "data": "use_flg",
                "render": function(data, type, full) {
                	return '<span class="switch" data-id="'+ full.id +'" data-in="'+full.id+'-1" data-type="1" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
                }
            },
            {
                "targets": [6],
                "data": "is_display",
                "render": function(data, type, full) {
                	return '<span class="switch" data-id="'+ full.id +'" data-in="'+full.id+'-2" data-type="2" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check-2"></span>';
                }
            }
        ],
        order: [[ 4, "asc"]]
    });
}

function changeArticleCategory(id){
	$.post('/article/get-article-category-detail',{id:id},success,'json');
	function success(data){
    	if(data.code == 200){
    		var result = data.val;
    		$('#article-category-modal').modal('show');
    		$("#category_id").val(id);
        	$('#category_title').val(result.category_title);
        	$('#category_pic').val(result.category_pic);
        	if(result.category_pic){
        		$('.show-img-btn').removeClass('hide');
        	}
        	$('#sort').val(result.sort);
        	$('#use_flg').val(result.use_flg);
        	$('#is_display').val(result.is_display);
        	$('#memo').val(result.memo);
        }else{
        	$.jBox.error(data.val, '错误');
        }
    }
}

function showImg(img_url){
	if(img_url == '' || img_url == null || img_url == undefined){
        $.jBox.error('数据为空', '错误');
        return false;
    }
	var json = {
        "title": "分类图片", //相册标题
        "id": 0, //相册id
        "start": 0, //初始显示的图片序号，默认0
        "data": [   //相册包含的图片，数组格式
            {
                "alt": "",//图片名
                "pid": 0, //图片id
                "src": $('#imageBathUrl').val() + img_url, //原图地址
                "thumb": "" //缩略图地址
            }
        ]
    };
    layer.photos({
        photos: json
    });
}

function table_list_reload(){
	var table = $('#tableid').DataTable();
    table.ajax.reload();
}

function changeArticleCategoryStatus(id,status,type){
	if(type == 2){
		title = '确定首页不显示此文章分类吗？';
		if(status == 1){
			title = '确定首页显示此文章分类吗？';
		}
	}else{
		title = '确定禁用此文章分类吗？';
		if(status == 1){
			title = '确定启用此文章分类吗？';
		}
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/article/change-article-category-status',{id:id,status:status,type:type},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200){
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	if(status == 1){
        		$('#tableid').find('span[data-in="'+id+'-'+type+'"]').children('input').prop('checked',false);
        	}else{
        		$('#tableid').find('span[data-in="'+id+'-'+type+'"]').children('input').prop('checked',true);
        	}
            $.jBox.close();
        }
    });
}

$('.add-article-category').on('click',function(){
	$('#article-category-modal').modal('show');
	$("#category_id").val('');
	$('#category_title').val('');
	$('#category_pic').val('');
	$('.show-img-btn').addClass('hide');
	$('#sort').val('');
	$('#use_flg').val(1);
	$('#is_display').val(1);
	$('#memo').val('');
});

$('#article_category_form').delegate('.upload-img-btn-main','click',function(){
    $('.upload_file_ot').trigger('click');
});

$('.upload_file_ot').change(function(){
    if(this.value != ''){
    	$('#upload_form_update').submit();
    }
});

$('#upload_form_update').submit(function(e){
    e.preventDefault();
    $(this).ajaxSubmit({
        success:function(data){
            var result = $.parseJSON(data);
            if(result.code == 500){
                $.jBox.error(result.val, '错误');
            }else{
            	if(result.val){
            		$('#category_pic').val(result.val);
            		$('.show-img-btn').removeClass('hide');
            	}
            	$('input[type=file]').closest('form').get(0).reset();
            }
        }
    });
});

$('#article-category-modal').on('click','.ajax_post_tag',function(){
	var form = $('#article_category_form');
	var category_title = $('#category_title').val();
    if(category_title == ''){
        $.jBox.error('请填写文章分类名称', '错误');
        return false;
    }
    var sort = $('#sort').val();
    if(sort == ''){
        $.jBox.error('请填写文章分类排序编号', '错误');
        return false;
    }
    $.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200){
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#article-category-modal').modal('hide');
                window.setTimeout(function () { table_list_reload(); }, 1000);
            }, 1000);
        }else{
            $.jBox.error(data.val, '错误');
        }
	}
});

$('table').on('click','.input-check , .input-check-2',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	var type = $(this).parent().attr('data-type');
	changeArticleCategoryStatus(id,use_flg,type);
});